/**
 * Author: ligj@mapbar.com
 * Create Time: 2018/01/12 10:41
 * Description:
 */

import Vue from "vue"

import {_setRoles_,isInRoles} from './roles';
const timeoutInstances = '_timeout_instances';
const intervalInstances = '_interval_instances';

Vue.mixin({
    methods: {
        $timeout(callback, millisec){
            let t = setTimeout(callback, millisec);
            this[timeoutInstances].push(t);
            return {
                cancel: function () {
                    t && clearTimeout(t);
                }
            }
        },
        $interval(callback, millisec){
            let t = setInterval(callback, millisec);
            this[intervalInstances].push(t);
            return {
                cancel: function () {
                    t && clearInterval(t);
                }
            }
        },
	    $isInRoles(roles){
		   return isInRoles(roles);
	    },
	    $setRoles(roles){
		    _setRoles_(roles);
	    }
    },
    beforeCreate(){
        this[timeoutInstances] = [];
        this[intervalInstances] = [];
    },
    destroyed(){
        this[timeoutInstances].forEach(t => {
            t && clearTimeout(t);
        })
        this[intervalInstances].forEach(t => {
            t && clearInterval(t);
        })
    }
});

export default {};
